//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Collections;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CA = ComponentArt.Web.UI;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;
using CSFolders = CommunityServer.Files.Components.Folders;

namespace CommunityServer.ControlPanel.FileAdmin
{
	/// <summary>
	///		Summary description for FolderGroupListControl.
	/// </summary>
	public class FolderGroupListControl : BaseFilesAdminGridControl
	{
		
		#region Child Controls

		protected CA.Grid Grid1;
		protected CheckBox chkConfirmInsert;
		protected CheckBox chkConfirmUpdate;
		protected CheckBox chkConfirmDelete;
		protected ResourceControl Resourcecontrol1;
		protected ModalLink NewGroup;

		#endregion
		protected CommunityServer.Controls.Modal Modal1;

		private const string EditGroupUrl = "FolderGroupForm.aspx?GroupID={0}";

		private void Page_Load(object sender, EventArgs e)
		{
			NewGroup.Text = ResourceManager.GetString("CP_FilesAdmin_FolderGroupListControl_AddNew");
			NewGroup.Url = string.Format(EditGroupUrl,"-1");

			if(!Page.IsPostBack && !FilesAdminPage.IsCallBack)
			{
				buildGrid();
				Grid1.DataBind();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			Grid.DeleteCommand += new CA.Grid.GridItemEventHandler(this.Grid_DeleteCommand);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void UpdateDb(CA.GridItem item, string command)
		{

			//Check to see that the user can administer
			//Ideally we will add conditional formatting on the grid to hide these options too
			if(!CSContext.Current.User.IsFileAdministrator)
				throw new CSException( CSExceptionType.AdministrationAccessDenied );

			Group group;

			switch (command)
			{
				case "DELETE":
					
					group = CSFolders.GetFolderGroup(int.Parse(item["GroupID"].ToString()), false, true);
					if(HasChildSections(group.GroupID))
						throw new ReadOnlyException(ResourceManager.GetString("CP_FilesAdmin_FolderGroupListControl_GroupNotEmptyException"));

					Groups.DeleteGroup(group);

					//flush the cache to make sure we pick up any changes
					CSFolders.GetFolderGroups(true, true, true);

					break;

				default: 
					throw new NotImplementedException(string.Concat("The {0} command has not yet been implemented!" ,command)); 
			}
		}

		private void Grid_DeleteCommand(object sender, CA.GridItemEventArgs e)
		{
			UpdateDb(e.Item, "DELETE"); 
		}

		protected override void buildGrid()
		{
			ArrayList groups = GetGroups();


			//The grid will only show checkboxes in edit mode if the data source is a DataTable
			Grid.DataSource = CADataConverter.ToDataTable(groups, typeof(Group));
			this.RecordCount = groups.Count;

			ApplyUserSettings();

		}

		protected override void ConfigureGrid()
		{
			AddGridPagerTemplate("Name");
			AddGridHrefTemplate(@"Folders.aspx?GroupID=## DataItem.GetMember(""GroupID"").Text ##","Name");
		}

		protected ArrayList GetGroups()
		{
			return CSFolders.GetFolderGroups(false, true, false);
		}

		/// <summary>
		/// Returns true if the Group has Sections
		/// </summary>
		protected bool HasChildSections(int groupID)
		{
			return CSFolders.GetFoldersByGroupID(groupID, true, false, false).Count > 0;
		}

	}
}
